﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CS633_Final_Project
{
    /// <summary>
    /// Used to represent the dual graph of the triangulation.
    /// </summary>
    class DualGraph
    {
        private Hashtable graph;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public DualGraph()
        {
            this.graph = new Hashtable();
        }

        /// <summary>
        /// Adds a triangle to the dual graph
        /// </summary>
        /// <param name="triangle">Triangle</param>
        public void AddTriangle(Triangle triangle)
        {
            foreach (Line edge in triangle.Edges)
            {
                if (graph.ContainsKey(edge))
                { // update existing record
                    GraphNode node = (GraphNode)graph[edge];
                    node.Triangles.Add(triangle);
                    graph.Remove(edge);
                    graph.Add(edge, node);
                }
                else
                { // create new record
                    GraphNode node = new GraphNode();
                    node.Triangles.Add(triangle);
                    graph.Add(edge, node);
                }
            }
        }

        /// <summary>
        /// Gets triangles associated with the edge.
        /// </summary>
        /// <param name="edge">Edge</param>
        /// <returns>Triangles associated with the edge</returns>
        public ArrayList GetTriangles(Line edge)
        {
            ArrayList triangles = null;

            if (graph.ContainsKey(edge))
            {
                GraphNode node = (GraphNode)graph[edge];
                triangles = node.Triangles;
            }

            return triangles;
        }

    }

    /// <summary>
    /// A node in the dual graph
    /// </summary>
    class GraphNode
    {
        private ArrayList triangles;

        /// <summary>
        /// Default Constructor
        /// </summary>
        public GraphNode()
        {
            triangles = new ArrayList();
        }

        /// <summary>
        /// The triangles of a graph node.
        /// </summary>
        public ArrayList Triangles
        {
            get { return this.triangles; }
            set { this.triangles = value; }
        }
    }
}
